import Vue from 'vue'
import FastClick from 'fastclick'
import VueRouter from 'vue-router'
import VueResource from 'vue-resource'
import Vuex from 'vuex'
import store from './vuex/store'
import routes from './router/router'
import App from './App'
import Home from './components/home/Home'

Vue.use(VueRouter)
Vue.use(VueResource)
Vue.use(Vuex)

FastClick.attach(document.body)


const router = new VueRouter({
  mode: 'history',
  base: __dirname,
  routes,
  scrollBehavior (to, from, savedPosition) {//路由切换时返回顶部
     if (savedPosition) {
    return savedPosition
  } else {
    const position = {}
    if (to.hash) {
      position.selector = to.hash
    }
    if (to.matched.some(m => m.meta.scrollToTop)) {
      position.x = 0
      position.y = 0
    }
    return position
  }
  }
})

//小数过滤器
Vue.filter('datadel', function(e) {
  var str = String(e.toFixed(2));
  while(str[str.length-1]=='0'){
    str=str.substring(0,str.length-1);
  }
  if(str[str.length-1]=='.'){
    str=str.substring(0,str.length-1);
  }
  return str;
})

//性别过滤器
Vue.filter('sexTrue', function(e) {
  let gender = e==1?'男':'女';
  return gender;
})

//余额详情过滤器
Vue.filter('cashway', function(e) {
  let income= e=="income"?true:false;
  return income;
})


Vue.http.interceptors.push(function(request, next) {
  // modify headers
  request.headers.set('Accept', 'application/json');
  request.headers.set('Authorization', 'Bearer '+ JSON.parse(localStorage.getItem('token')));
  // continue to next interceptor
  next();
});


/* eslint-disable no-new */
new Vue({
  store,
  router,
  render: h => h(App)
}).$mount('#app-box')
